package validator;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import model.type.SubjectAttributeType;

public class AttributeValidator {
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM dd, yyyy", Locale.ENGLISH);
	
	Integer subject;
	Integer attributeType;
	Integer subjectType;
	Integer orderby;
	Integer dataType;
	
	Integer intValue;
	String stringValue;
	Date dateValue;
	
	public AttributeValidator(String subject, String attributeType, String subjectType, String orderby, String value){
		try{
			this.subject = Integer.parseInt(subject);
		} catch (NullPointerException | NumberFormatException e){	}
		try{
			this.attributeType = Integer.parseInt(attributeType);
		} catch (NullPointerException | NumberFormatException e){	}
		try{
			this.subjectType = Integer.parseInt(subjectType);
		} catch (NullPointerException | NumberFormatException e){	}
		try{
			this.orderby = Integer.parseInt(orderby);
		} catch (NullPointerException | NumberFormatException e){	}
		
		dataType = SubjectAttributeType.getSubjectAttributeType(this.attributeType).getDataType();
		try{
			switch(dataType){
				case 1: stringValue = !value.isEmpty() ? value : null; break;
				case 2: intValue = Integer.parseInt(value); break;
				case 3: dateValue = dateFormat.parse(value); break;
			}
		} catch(ParseException | NumberFormatException | NullPointerException e){}				
	}
	
	public boolean isValid(){
		return subject != null && attributeType != null && subjectType != null
				&& orderby != null && dataType != null && 
				(intValue != null || stringValue != null || dateValue != null);
	}

	public Integer getSubject() {
		return subject;
	}

	public Integer getAttributeType() {
		return attributeType;
	}

	public Integer getSubjectType() {
		return subjectType;
	}

	public Integer getOrderby() {
		return orderby;
	}

	public Integer getDataType() {
		return dataType;
	}

	public Integer getIntValue() {
		return intValue;
	}

	public String getStringValue() {
		return stringValue;
	}

	public Date getDateValue() {
		return dateValue;
	}
	
	
}
